/*

=============================================================================

Copyright (c) 2010 Xiotex Studios Ltd.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
 
The end-user documentation included with the redistribution, if any, must
include the following acknowledgment: "This product includes software
developed by Xiotex Studios Ltd (http://xiotexstudios.com) and Oladotun Rominiy",
in the same place and form as other third-party acknowledgments. Alternately,
this acknowledgment may appear in the software itself, in the same form and
location as other such third-party acknowledgments.
 
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE. 

=============================================================================

File:		CSWFStream.h
Author:		Byron Atkinson-Jones

Update(s):	(Ola)Dotun Rominiyi (28/10/2010) - replaced built-in types for
											   basic types
											 - shuffled includes for better
											   compilation times
											 - switched initialisation to
											   implement the CSWFAbstractStream
										     - switched math functions to use
											   SWFMath.h functions

=============================================================================

*/

#ifndef __CSWFSTREAM_H__
#define __CSWFSTREAM_H__

#include "SWFTypes.h"


class CSWFAbstractStream;

class CSWFStream
{

public:
	CSWFStream();
	~CSWFStream();
	bool Initialise(CSWFAbstractStream* pSourceStream);
	void ShutDown();
	void StartStream(swf_stream pStream);
	sInt32 GetSignedBits(uInt32 dwNumBits);
	uInt32 GetBits(uInt32 dwNumBits);
	void GetShortTag(PSWF_TAG_SHORT pTag, swf_stream pStream);
	uInt32 GetWORD(swf_stream pStream);
	uInt32 GetInt(swf_stream pStream);
	uChar8 GetBYTE(swf_stream pStream);
	void GetIntTag(PSWF_TAG_sInt32 pTag, swf_stream pStream);
	uInt32 GetRect(PSWF_RECT rect, swf_stream pStream);
	void ExtractRGBA(PSWF_RGBA pRGBA, swf_stream *pStream);
	void ExtractRGB(PSWF_RGBA pRGBA, swf_stream *pStream);
	void ExtractMatrix(PSWF_MATRIX_RECORD pMatrix, swf_stream *pStream);
	void ExtractColourTransformRGBA(PSWF_COLOUR_TRANSFORM pRGBA, swf_stream *pStream);
	void ExtractColourTransformRGB(PSWF_COLOUR_TRANSFORM pRGBA, swf_stream *pStream);
	void ExtractPlaceObject2(PSWF_TAG pTag, swf_stream pStream);
	void ExtractRemoveObject(PSWF_TAG pTag, swf_stream pStream);
	void ExtractDefineShape(PSWF_TAG pTag, swf_stream pStream);
	void ExtractDefineSprite(PSWF_TAG pTag, swf_stream pStream, CSWFTagList* pTagList);


public:
	inline swf_stream GetStreamBeginning() { return m_pBuffer; }
	inline uInt32 GetNumBytesRead() { return m_dwNumBytes; }
	
	inline void SetMatrix(float32 fScaleX, float32 fScaleY, float32 fRotSkew0, float32 fRotSkew1, float32 fX, float32 fY)
	{
		m_mat[0][0] = fScaleX;
		m_mat[1][0] = fRotSkew0;
		m_mat[0][1] = fRotSkew1;
		m_mat[1][1] = fScaleY;
		m_mat[0][2] = fX;
		m_mat[1][2] = fY;
	}
	
	inline float32 GetScaleX()
	{
		return swfSqrtf(m_mat[0][0] * m_mat[0][0] + m_mat[1][0] * m_mat[1][0]);
	}
	
	inline float32 GetScaleY()
	{
		return swfSqrtf(m_mat[1][1] * m_mat[1][1] + m_mat[0][1] * m_mat[0][1]);
	}
	
	inline float32 GetAngle()
	{
		return swfArcTan2f(m_mat[1][0], m_mat[0][0]);
	}
	
	inline float32 GetTranslateX()
	{
		return m_mat[0][2];
	}
	
	inline float32 GetTranslateY()
	{
		return m_mat[1][2];
	}

protected:
	CSWFAbstractStream*	m_pIn;
	swf_stream		m_pBuffer;
	uInt32			m_dwLength;
	swf_stream		m_pStream;
	swf_stream		m_pStreamStart;
	uInt32			m_dwBitPosition;
	uInt32			m_dwBitBuffer;
	uInt32			m_dwNumBytes;
	float32			m_mat[2][3];
};

#endif // __CSWFSTREAM_H__